package com.bawei.module_home

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.activityViewModels
import androidx.fragment.app.viewModels
import androidx.viewpager2.widget.ViewPager2.OnPageChangeCallback
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.bawei.lib_base.RoutePath
import com.bawei.lib_common.mvi.BaseMviFragment
import com.bawei.module_home.adapter.HomePageAdapter
import com.bawei.module_home.databinding.FragmentHomeBinding
import com.bawei.module_home.entriy.SimpleTypeBeanItem
import com.bawei.module_home.intent.HomeViewIntent
import com.bawei.module_home.state.HomeUiState
import com.bawei.module_home.vm.HomeViewmodel
import com.google.android.material.tabs.TabLayoutMediator
import dagger.hilt.android.AndroidEntryPoint

@AndroidEntryPoint
@Route(path = RoutePath.PATH_FRAGMENT_HOME)
class HomeFragment : BaseMviFragment<FragmentHomeBinding>() {
    private val mViewModel:HomeViewmodel by activityViewModels()

    private val mtab= mutableListOf<SimpleTypeBeanItem>()
private val mListFragment= mutableListOf<HomeListFragment>()
    private lateinit var homePageAdapter: HomePageAdapter
    private val searchHotWords= listOf("热门视频","搞笑视频","广场舞视频")
    override fun getLayoutId(): Int {
        return R.layout.fragment_home
    }

    override fun initViews() {
     homePageAdapter= HomePageAdapter(mListFragment,this.childFragmentManager,lifecycle)
        mDatatBinding.vp2Home.adapter=homePageAdapter
        mDatatBinding.vp2Home.offscreenPageLimit=1
        //关联
        TabLayoutMediator(mDatatBinding.tabTypeLayout,mDatatBinding.vp2Home){tab,position->
            tab.text=mtab[position].typename
        }.attach()
        mDatatBinding.vp2Home.registerOnPageChangeCallback(object :OnPageChangeCallback(){
            override fun onPageSelected(position: Int) {
                super.onPageSelected(position)
                if(mListFragment[position].isAdded){
                    mListFragment[position].lazyLoadData()
                }else{
                    mDatatBinding.vp2Home.postDelayed({
                     mListFragment[position].lazyLoadData()
                    },300)
                }
            }
        })


    }

    /**
     * 发送意图
     */
    override suspend fun sendViewIntent() {
        mViewModel.channel.send(HomeViewIntent.LoadVideType)
    }

    override suspend fun collectUIState() {
        mViewModel.uiStateFlow.collect{
            when(it){
                is HomeUiState.LoadSimpleTypeSuccess->{
                  initSimpleTypeLayout(it.data)
                }
                is HomeUiState.LoadSimpleTypeFailed->{
                 showToastMsg(it.meg)
                }
                else->{

                }
            }
        }
    }

    private fun initSimpleTypeLayout(data: List<SimpleTypeBeanItem>) {
      if(data.isNotEmpty()){
          //添加分类集合
          mtab.add(SimpleTypeBeanItem("",0,-1,0,"","推荐"))
            mListFragment.add(HomeListFragment.newInstance("",-1))
          //其他分类
          data.forEach {
              mtab.add(it)
              mListFragment.add(HomeListFragment.newInstance(it.channelid,it.id))
          }

          //创建tab
          mtab.forEach {
            mDatatBinding.tabTypeLayout.addTab(mDatatBinding.tabTypeLayout.newTab().setText(it.typename))
          }
          homePageAdapter.notifyDataSetChanged()
      }

    }


}

